﻿# Network Utilities PowerShell Module
# http://powershell.codeplex.com

Function Test-TCPPort
{
	Param
	(
		[ValidateNotNullOrEmpty()]
		[Parameter(Mandatory=$true)] [string] $EndPoint,
		[Parameter(Mandatory=$true)] [int64] $Port
	)

	trap { return $false }
	$Socket = New-Object System.Net.Sockets.TCPClient($EndPoint,$Port)
	$Socket.Close()
	return $true
}

Function Enable-Monitoring
{
	Param
	(
		[ValidateNotNullOrEmpty()]
		[Parameter(Mandatory = $true)] [string] $Url,
		[Parameter(Mandatory = $true)] [int64] $Port,
		[Parameter(Mandatory = $true)] [string] $Client,
		[Parameter(Mandatory = $true)] [string] $Column
	)

	try
	{
		$Socket = New-Object System.Net.Sockets.TCPClient($Url,$Port)
		$Stream = $Socket.GetStream()
		$Writer = New-Object System.IO.StreamWriter($Stream)
		$Writer.AutoFlush = $true
		$Writer.NewLine = $true
		$Writer.Write("enable $Client.$Column")
		$Socket.Close()
	}
	catch
	{
		Write-Host "ERROR :`r`n$_"
	}
}

Function Disable-Monitoring
{
	Param
	(
		[ValidateNotNullOrEmpty()]
		[Parameter(Mandatory = $true)] [string] $Url,
		[Parameter(Mandatory = $true)] [int64] $Port,
		[Parameter(Mandatory = $true)] [string] $Client,
		[Parameter(Mandatory = $true)] [string] $Column,
		[string] $Message,
		[string] $Duration
	)

	try
	{
		if ($Duration)
		{
			$Message = "disable $Client.$Column $Duration $Message"
		}
		else
		{
			$Message = "disable $Client.$Column"
		}
		$Socket = New-Object System.Net.Sockets.TCPClient($Url,$Port)
		$Stream = $Socket.GetStream()
		$Writer = New-Object System.IO.StreamWriter($Stream)
		$Writer.AutoFlush = $true
		$Writer.NewLine = $true
		$Writer.Write($Message)
		$Socket.Close()
	}
	catch
	{
		Write-Host "ERROR :`r`n$_"
	}
}

Function Write-Monitoring
{
	Param
	(
		[ValidateNotNullOrEmpty()]
		[Parameter(Mandatory = $true)] [string] $Url,
		[Parameter(Mandatory = $true)] [int64] $Port,
		[Parameter(Mandatory = $true)] [string] $Client,
		[Parameter(Mandatory = $true)] [string] $Column,
		[Parameter(Mandatory = $true)] [string] $Message,
		[ValidateSet('red','yellow','green')] [string] $Color = "green"
	)

	try
	{
		$Socket = New-Object System.Net.Sockets.TCPClient($Url,$Port)
		$Stream = $Socket.GetStream()
		$Writer = New-Object System.IO.StreamWriter($Stream)
		$Writer.AutoFlush = $true
		$Writer.NewLine = $true
		$Writer.Write("status $Client.$Column $Color $Message")
		$Socket.Close()
	}
	catch
	{
		Write-Host "ERROR :`r`n$_"
	}
}